home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / mcu11 / examples.arc / EX8 < prev    next >
Text File  |  1988-11-29  |  9KB  |  188 lines

  1. * Register definitions for 68HC11 registers (used for Ex 8-1 & 8-2)
  2. PORTD  EQU   $1008    Port D data register
  3. *                     " -  , -  ,SS* ,SCK ;MOSI,MISO,TxD ,RxD "
  4. DDRD   EQU   $1009    Port D data direction
  5. SPCR   EQU   $1028    SPI control register
  6. *                     "SPIE,SPE ,DWOM,MSTR;CPOL,CPHA,SPR1,SPR0"
  7. SPSR   EQU   $1029    SPI status register
  8. *                     "SPIF,WCOL, -  ,MODF; -  , -  , -  , -  "
  9. SPDR   EQU   $102A    SPI data register; Read-Buffer; Write-Shifter
  10.  
  11. * RAM variables (DAx used by Ex 8-1 & 8-2, SPIx used only by 8-1)
  12. DA1    EQU   $00      6-bit val for D/A ch 1 "-,-,15,14;13,12,11,10"
  13. DA2    EQU   $01      6-bit val for D/A ch 2 "-,-,25,24;23,22,21,20"
  14. DA3    EQU   $02      6-bit val for D/A ch 3 "-,-,35,34;33,32,31,30"
  15. DA4    EQU   $03      6-bit val for D/A ch 4 "-,-,45,44;43,42,41,40"
  16. DA5    EQU   $04      6-bit val for D/A ch 5 "-,-,55,54;53,52,51,50"
  17. DA6    EQU   $05      6-bit val for D/A ch 6 "-,-,65,64;63,62,61,60"
  18. * Upper 2 bits of DAx should be 0 but will be ignored.
  19. SPI1   EQU   $06      SPI packed byte 1 "--,--,--,--;65,64,63,62"
  20. SPI2   EQU   $07      SPI packed byte 2 "61,60,55,54;53,52,51,50"
  21. SPI3   EQU   $08      SPI packed byte 3 "45,44,43,42;41,40,35,34"
  22. SPI4   EQU   $09      SPI packed byte 4 "33,32,31,30;25,24,23,22"
  23. SPI5   EQU   $0A      SPI packed byte 5 "21,20,15,14;13,12,11,10"
  24. * NOTE: Upper 4 bits of SPI1 are unused extras but will be 0.
  25.  
  26. ***********************************************************************
  27. * Example 8-1                                                         *
  28. *                                                                     *
  29. *  This example program uses the on-chip hardware SPI to drive an     *
  30. * MC144110 six channel D to A converter peripheral.                   *
  31. *                                                                     *
  32. *  To try Ex 8-1, connect MC144110 to Port D pins on EVB, load        *
  33. * program into EVB RAM, manually enter data for DA1 to DA6 and        *
  34. * execute a GO to $C000.                                              *
  35. ***********************************************************************
  36.  
  37.        ORG   $C000    Start of user's RAM in EVB
  38. INIT1  LDS   #$CFFF   Top of C page RAM
  39.        LDAA  #$2F     -,-,1,0;1,1,1,1
  40. *                     SS*-Hi, SCK-Lo, MOSI-Hi
  41.        STAA  PORTD    So SS stays high when DDRD5 set
  42.        LDAA  #$38     -,-,1,1;1,0,0,0
  43.        STAA  DDRD     SS*, SCK, MOSI - Outs
  44. *                     MISO, TxD, RxD - Ins
  45. * DDRD5=1 so SS* pin is a general purpose output
  46.        LDAA  #$57
  47.        STAA  SPCR     SPI on as Master, CPHA=1, CPOL=0
  48. *                     E/32 Clk rate
  49. ***
  50. * Following two instructions call main routine for Ex 8-1
  51. ***
  52.        BSR   UPDAT1   Xfer 5 8-bit words to MC144110
  53.        JMP   $E000    Restart BUFFALO
  54. ***
  55. UPDAT1 PSHX           Save registers X and A
  56.        PSHY
  57.        PSHA
  58.        BSR   REFORM   Reformat data so SPI can xfer it
  59.        LDX   #SPI1    Point at 1st byte to send via SPI
  60.        LDY   #$1000   Point at on-chip registers
  61.        BCLR  PORTD,Y %00100000  Drive SS* low
  62. TFRLP1 LDAA  0,X      Get a byte to transfer via SPI
  63.        STAA  SPDR     Write SPI data reg to start xfer
  64. WAIT1  LDAA  SPSR     Loop to wait for SPIF
  65.        BPL   WAIT1    SPIF is in MSB of SPSR
  66. *                     (when SPIF set, SPSR neg)
  67.        INX            Point to next SPI byte
  68.        CPX   #SPI5+1  Done yet ?
  69.        BNE   TFRLP1   If not, tfr another byte
  70.        BSET  PORTD,Y %00100000  Drive SS* high
  71.        PULA           When done, restore regs X, Y & A
  72.        PULY
  73.        PULX
  74.        RTS            ** Return **
  75.  
  76. ***********************************************************************
  77. * REFORM - This subroutine reformats six 6 bit values into five 8 bit *
  78. *          values so they can be sent by the SPI system.              *
  79. *                                                                     *
  80. *  The MC144110 needs 36 bits of information which is not a multiple  *
  81. * of 8 bits; however, we can send five 8 bit words (40 bits) and the  *
  82. * MC144110 will use only the last 36 bits.                            *
  83. *                                                                     *
  84. *       Original format               Change to this format           *
  85. *    DA1 "-,-,15,14;13,12,11,10"   SPI1 "--,--,--,--;65,64,63,62"     *
  86. *    DA2 "-,-,25,24;23,22,21,20"   SPI2 "61,60,55,54;53,52,51,50"     *
  87. *    DA3 "-,-,35,34;33,32,31,30"   SPI3 "45,44,43,42;41,40,35,34"     *
  88. *    DA4 "-,-,45,44;43,42,41,40"   SPI4 "33,32,31,30;25,24,23,22"     *
  89. *    DA5 "-,-,55,54;53,52,51,50"   SPI5 "21,20,15,14;13,12,11,10"     *
  90. *    DA6 "-,-,65,64;63,62,61,60"                                      *
  91. ***********************************************************************
  92.  
  93. REFORM PSHB           Save registers B and A
  94.        PSHA
  95.        LDAA  DA1      A="--,--,15,14;13,12,11,10"
  96.        ASLA           A="--,15,14,13;12,11,10, 0"
  97.        ASLA           A="15,14,13,12;11,10, 0, 0"
  98.        LDAB  DA2      B="--,--,25,24;23,22,21,20"
  99.        ANDB  #$3F     B=" 0, 0,25,24;23,22,21,20"
  100.        LSRB           B=" 0, 0, 0,25;24,23,22,21" C="20
  101.        RORA           A="20,15,14,13;12,11,10, 0"
  102.        LSRB           B=" 0, 0, 0, 0;25,24,23,22" C="21
  103.        RORA           A="21,20,15,14;13,12,11,10"
  104.        STAA  SPI5     SPI5 is done
  105.        STAB  SPI4     SPI4 intermediate value
  106.        LDAA  DA4      A="--,--,45,44;43,42,41,40"
  107.        LDAB  DA3      B="--,--,35,34;33,32,31,30"
  108.        ASLB           B="--,35,34,33;32,31,30, 0"
  109.        ASLB           B="35,34,33,32;31,30, 0, 0"
  110.        ASLB           B="34,33,32,31;30, 0, 0, 0" C="35
  111.        ROLA           A="--,45,44,43;42,41,40,35"
  112.        ASLB           B="33,32,31,30; 0, 0, 0, 0" C="34
  113.        ROLA           A="45,44,43,42;41,40,35,34"
  114.        ORAB  SPI4     B="33,32,31,30;25,24,23,22"
  115.        STAB  SPI4     SPI4 now complete
  116.        STAA  SPI3     SPI3 done
  117.        LDAA  DA5      A="--,--,55,54;53,52,51,50"
  118.        ASLA           A="--,55,54,53;52,51,50, 0"
  119.        ASLA           A="55,54,53,52;51,50, 0, 0"
  120.        LDAB  DA6      B="--,--,65,64;63,62,61,60"
  121.        ANDB  #$3F     B=" 0, 0,65,64;63,62,61,60"
  122.        LSRB           B=" 0, 0, 0,65;64,63,62,61" C="60
  123.        RORA           A="60,55,54,53;52,51,50, 0"
  124.        LSRB           B=" 0, 0, 0, 0;65,64,63,62" C="61
  125.        RORA           A="61,60,55,54;53,52,51,50"
  126.        STAA  SPI2     SPI2 done
  127.        STAB  SPI1     SPI1 done
  128.        PULA           Restore registers A and B
  129.        PULB
  130.        RTS            ** Return **
  131.  
  132. ***********************************************************************
  133. * Example 8-2                                                         *
  134. *                                                                     *
  135. *  This example program uses a software equivalent of the SPI to      *
  136. * drive an MC144110 six channel D to A converter peripheral.  The     *
  137. * physical hookup is the same as that of the previous example to make *
  138. * comparisons easier.                                                 *
  139. *                                                                     *
  140. *  To try Ex 8-2, connect MC144110 to Port D pins on EVB, load        *
  141. * program into EVB RAM, manually enter data for DA1 to DA6 and        *
  142. * execute a GO to $C100.                                              *
  143. ***********************************************************************
  144.  
  145.        ORG   $C100
  146. INIT2  LDS   #$CFFF   Top of C page RAM
  147.        LDAA  #$2F     -,-,1,0;1,1,1,1
  148.        STAA  PORTD    PD5/SS*-Lo,PD4/SCK-Lo,PD3/MOSI-Hi
  149.        LDAA  #$38     -,-,1,1;1,0,0,0
  150.        STAA  DDRD     PD5, PD4, PD3 =Outs; Others =Ins
  151.        LDAA  #$04
  152. *            "SPIE,SPE,DWOM,MSTR;CPOL,CPHA,SPR1,SPR0"
  153.        STAA  SPCR     Make sure SPI off
  154. ***
  155. * Following two instructions call main routine for Ex 8-2
  156. ***
  157.        BSR   UPDAT2   Xfer six 6 bit words to MC144110
  158.        JMP   $E000    Restart BUFFALO
  159. ***
  160. UPDAT2 PSHX           Save X, Y and A
  161.        PSHY
  162.        PSHA
  163.        LDY   #DA6     Point at 1st D/A value to xfer.
  164.        LDX   #$1000   Point at register area.
  165. TFRLP2 LDAA  #$20     1st pntr to MSB of 6 bit data val
  166.        BCLR  PORTD,X %00100000  PD5(SS*) Falling edge
  167.        NOP            Need more dly for MC144110 specs.
  168.        NOP
  169. NXTBIT BSET  PORTD,X %00010000  PD4(SCK) Rising edge
  170.        BITA  0,Y      Test sense of bit to be sent
  171.        BEQ   ZBIT     If zero skip around
  172.        BSET  PORTD,X %00001000  PD3(MOSI) Hi bit
  173.        BRA   ENDBIT
  174. ZBIT   BCLR  PORTD,X %00001000  PD3(MOSI) Lo bit
  175.        BRA   ENDBIT   Want Lo time to match Hi time
  176. ENDBIT BCLR  PORTD,X %00010000  PD4(SCK) Falling edge
  177.        LSRA           Pointer to nxt lower bit position
  178.        BNE   NXTBIT   Done if pointer shifted past LSB
  179.        DEY            Point at next value to send
  180.        CPY   #DA1-1   Done yet ?
  181.        BNE   TFRLP2   If not go back to top of loop
  182.        BSET  PORTD,X %00100000  PD5(SS*) Rising edge
  183.        PULA           Restore X, Y and A
  184.        PULY
  185.        PULX
  186.        RTS            ** RETURN **
  187.  
  188.